<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <cpn></cpn>
        <cpn></cpn>
        <cpn ref="aaa"></cpn>
        <cpn></cpn>
        <button @click="btnClick">按钮</button>
    </div>

    <template id="cpn">
        <div>我是子组件</div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                message: "你好呀",
            },
            components: {
                cpn: {
                    template: "#cpn",
                    methods: {
                        showMessage() {
                            console.log("showMessage");
                        }
                    },
                    data(){
                        return {
                            name : "我是子组件的name"
                        }
                    }
                }
            },
            methods: {
                btnClick() {
                    // 1.通过$children数组获取子组件有局限性，例如：当组件顺序发生变化，通过index获取的子组件也发生了变化
                    console.log(this.$children[0]);
                    this.$children[0].showMessage();
                    console.log(this.$children[0].name);

                    // 2.通过$refs精确获取子组件
                    console.log(this.$refs.aaa.name);
                }
            }
        })
    </script>
</body>

</html>